//
//  WbHomeController.swift
//  Weibo
//
//  Created by HH on 16/11/30.
//  Copyright © 2016年 Weibo. All rights reserved.
//

import UIKit

let WbStatusesCellID = "WbStatusesCellID"

class WbHomeController: WbVisitorController {
    
    var homeViewModel = WbHomeViewModel.sharedHomeViewModel

    override func viewDidLoad() {
        super.viewDidLoad()
        
        if isLogin == false {
            visitorView?.updateInfo(title: nil, image: nil)
        }else{
            self.setupUI()
            loadData()
        }
        
    }
    
    
    //TipView
    lazy private var tipView:UILabel = {
        
        let label = UILabel(text: "", font: 14, textColor: UIColor.white)
        
        label.backgroundColor = UIColor.orange
        
        label.alpha = 0.7
        
        label.textAlignment = .center
        
        return label
        
    }()

    
    //MARK:小菊花
    lazy private var activityIndictiorView:UIActivityIndicatorView = {
        
       let activityIndictior = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)
        
        activityIndictior.isHidden = false
        
        activityIndictior.color = UIColor.brown
        
        return activityIndictior
    }()
    
    //下拉刷新
    lazy private var pullDownRefresh:WbRefreshControl = {
       
        let refresh = WbRefreshControl()
        
        refresh.addTarget(self, action: #selector(loadData), for: .valueChanged)
        
        return refresh
        
    }()

    
    //MARK:获取数据
    func loadData(){

        WbHomeViewModel.sharedHomeViewModel.loadRequestStatuses_friends_timeline(isPullUp:activityIndictiorView.isAnimating) { (isSucess,message) in
            
            //要通过判断上拉刷新的风火轮是否转动来判断是否开启tip动画
            if self.activityIndictiorView.isAnimating == false{
                //开启Tip动画
                self.startTipViewAnimation(message: message)
            }
            
            //停止刷新动画,为了不做复杂的判断,将所有动画都停止
            self.endRefreshing()
                    if isSucess{
                        
                        self.tableView.reloadData()
 
                    }else{
                        
                        print("获取数据失败")
              }
         }
       
    }
    
    
    //开启Tip动画
    func startTipViewAnimation(message:String){
        
        if self.activityIndictiorView.isAnimating == false{
            
            self.tipView.text = message
            //                        print(self.navigationController?.navigationBar.frame.maxY)
            
            UIView.animate(withDuration: 1.0, animations: {
                
                self.tipView.y = (self.navigationController?.navigationBar.frame.maxY)!
                
                
            }, completion: { (isSucess) in
                
                UIView.animate(withDuration: 1.0, animations: {
                    self.tipView.y = (self.navigationController?.navigationBar.frame.maxY)! - self.tipView.height
                })
            })
            
        }
    }
    
    
    
    override func setupUI() {
        
        tableView.register(WbStatusesCell.self, forCellReuseIdentifier: WbStatusesCellID)
        
        tableView.rowHeight = UITableViewAutomaticDimension
        
        tableView.estimatedRowHeight = 200
        
        tableView.separatorStyle = .none
        
        tableView.tableFooterView = activityIndictiorView
        
        tableView.addSubview(pullDownRefresh)
        
        //添加tipView
        if let navController = self.navigationController {
            
            navController.view.insertSubview(tipView, belowSubview: navController.navigationBar)
            
            tipView.frame = CGRect(x: 0, y: navController.navigationBar.frame.maxY - 40, width: ScreenWidth, height: 40)
            
        }
        
        
    }
    
    //MARK:停止刷新
    private func endRefreshing(){
        
        activityIndictiorView.stopAnimating()
        pullDownRefresh.endRefresh()
         
    
    }
    
    //MARK:将要显示
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if indexPath.row == self.homeViewModel.statuesList.count - 1,!activityIndictiorView.isAnimating {
            
           self.activityIndictiorView.startAnimating()
            self.loadData()
            print("这是最后一个Cell")
        }
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return self.homeViewModel.statuesList.count
        
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: WbStatusesCellID, for: indexPath) as! WbStatusesCell
        
        cell.statusesViewModel = self.homeViewModel.statuesList[indexPath.row]
        
        return cell
        
    }
    
   
    

}


